//获取应用实例
var app = getApp()
Page({

  data: {
    sexValue:'1',
    payType:'1',
    noteContent:''
  },
  onLoad: function (e) {
    var that = this;
    if(e){
      that.setData({
        id:e.id,
        cardName: e.cardName,
        price:e.price,
        type:e.type
      })
    }
    that.getCurrentDate();
    that.getUserInfor();
    console.log(app.globalData.userInfo);
  },
  //获得当前的日期
  getCurrentDate:function(){
    var that = this;
    var myDate = new Date();
    //获取当前年
    var year = myDate.getFullYear();
    //获取当前月
    var month = myDate.getMonth() + 1;
    //获取当前日
    var day = myDate.getDate(); 
    var str = year+'-'+month+'-'+day;
    that.setData({
      date: str
    });
    console.log(that.data.date);
  },
  //获得所填信息
  getUserInfor:function(){
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_store_member_info',
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data);
          that.setData({
            name:res.data.data.name,
            sexValue: res.data.data.gender,
            date: res.data.data.birthday,
            telphone: res.data.data.telphone,
            coin:res.data.data.coin
          })
        } else {
          console.log(res.data)
        }
      },
      complete: function () {
      }
    });
  },
  //获得生日
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  //获得支付方式
  radioChange: function (e) {
  var that = this;
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    that.setData({
      payType:e.detail.value
    })
  },
  sexSelect:function(e){
    var that = this;
    var sex = e.currentTarget.dataset.sex;
    that.setData({
      sexValue:sex
    })
  },
  nameChange:function(e){
    var that = this;
    that.setData({
      name:e.detail.value
    })
    console.log(that.data.name);
  },
  phoneChange: function (e) {
    var that = this;
    console.log('电话');
    console.log(e.detail.value);
    that.setData({
      telphone: e.detail.value
    })
    console.log(that.data.telphone);
  },
  //备注信息 
  noteChange(e){
    console.log('备注信息',e.detail.value)
    this.setData({
      noteContent:e.detail.value
    })
  },
  submitInfor: function () {
    var that = this;
    app.getSubId(that, 'applet_buy_member_card', 'tosubmitInfor');
  },
  tosubmitInfor:function(){
    var that = this;
    var data = {};
    var id = that.data.id;
    var type = that.data.type;
    var name = that.data.name;
    var gender  = that.data.sexValue;
    var birthday = that.data.date;
    var telphone = that.data.telphone;
    var payType = that.data.payType;
    var noteContent = that.data.noteContent;
    console.log(name);
    console.log(gender);
    console.log(birthday);
    console.log(telphone);
    console.log(payType);
    data.map = 'applet_buy_member_card';
    data.cardid = id;
    data.type = type;
    data.name = name;
    data.gender = gender;
    data.birthday = birthday;
    data.telphone = telphone;
    data.payType = payType;
    data.remark= noteContent;
    data.scene = app.globalData.enterScene;//交易组件-场景值
    if (!data.telphone) {
      app.errorTip(that, "请输入手机号码", 2000);
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确认立即支付吗？',
      success(result) {
        if (result.confirm) {
          wx.request({
            url: app.globalData.requestUrl,
            data: data,
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                if (res.data.data.status == 'dzf') {
                  that.orderPay(res.data.data.tid);
                } else if (res.data.data.status == 'zfcg') {
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 100)
                }
              } else {
                console.log(res.data);
                app.errorTip(that, res.data.em, 2000);
              }
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else if (result.cancel) {
          console.log('用户点击取消')
        }
      }
    }) 
  },
  orderPay: function (tid) {
    var that = this;
    var data = {};
    data.map = 'applet_pay_member_card';
    data.tid = tid;
    console.log(data);
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 1000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var params = res.data.data;
          app.commonRequestPayment(params)
          .then(res => {
            //支付成功之后的操作处理
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 100)
          }, err => {
            console.log(err);
          })
          // wx.requestPayment({
          //   'appId': res.data.data.appId,
          //   'timeStamp': res.data.data.timeStamp,
          //   'nonceStr': res.data.data.nonceStr,
          //   'package': res.data.data.package,
          //   'signType': res.data.data.signType,
          //   'paySign': res.data.data.paySign,
          //   'success': function (res) {
          //     // wx.redirectTo({
          //     //   url: '/pages/cardPaySuccess/cardPaySuccess?orderid=' + data.tid
          //     // })
          //     setTimeout(function () {
          //       wx.navigateBack({
          //         delta: 1
          //       })
          //     }, 100)
          //   },
          //   'fail': function (res) {
          //     console.log(res);
          //   }
          // });
        } else {
          console.log(res.data);
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
      }
    });
  },
})